#include "Receiver.h"

MyEventReceiver::MyEventReceiver(IrrlichtDevice* in_device, ICameraSceneNode* in_camera, ICameraSceneNode* in_third_camera) 
{
	device = in_device;
	camera = in_camera;
	third_person_camera = in_third_camera;
	cycle_point = 0;
	reload = false;
	draw_path = false;
	clear_path = false;
	node_to_move = 0;

	u32 name = 753790439;
	node_names.push_front(name);
	name = 1706498636;
	node_names.push_front(name);
	name = 735155871;
	node_names.push_front(name);
	name = 1966789447;
	node_names.push_front(name);
	name = 963063589;
	node_names.push_front(name);
	name = 184863392;
	node_names.push_front(name);
	name = 1974283966;
	node_names.push_front(name);
	name_counter = 0;
	move_obj = false;
}

void MyEventReceiver::SetCameras(ICameraSceneNode* in_camera, ICameraSceneNode* in_third_camera)
{
	camera = in_camera;
	third_person_camera = in_third_camera;
}

void MyEventReceiver::SetDevice(IrrlichtDevice* in_device)
{
	device = in_device;
}


MyEventReceiver::~MyEventReceiver(){}

bool MyEventReceiver::OnEvent(const SEvent& event)
{
				
		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_1 && event.KeyInput.PressedDown)
		{	
			device->getSceneManager()->setActiveCamera(camera);	
			return true;
		}

        if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_2 && event.KeyInput.PressedDown)
		{	
			device->getSceneManager()->setActiveCamera(third_person_camera);	
			return true;
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_ESCAPE && event.KeyInput.PressedDown)
		{	
			device->closeDevice();
			return true;
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_F && event.KeyInput.PressedDown)
		{	
			if(!draw_path)
			{
				draw_path = true;
			}

			else
			{
				draw_path = false;
			}
			return true;
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_T && event.KeyInput.PressedDown)
		{	
			clear_path = true;

			return true;
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_R && event.KeyInput.PressedDown)
		{	
			cycle_point++;
			if(cycle_point > 3) {cycle_point = 0;}
			reload = true;

			return true;
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_Q && event.KeyInput.PressedDown)
		{	
			if(!move_obj)
			{
				move_obj = true;
			}
			if(node_to_move != 0)
			{
				node_to_move->setMaterialFlag(video::EMF_LIGHTING, false);
			}
			
			node_to_move = device->getSceneManager()->getSceneNodeFromId(node_names[name_counter]);
			name_counter++;
			node_to_move->setMaterialFlag(video::EMF_LIGHTING, true);
			if(name_counter > 6)
			{
				name_counter = 0;
			}
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_E && event.KeyInput.PressedDown)
		{	
			if(move_obj)
			{
				move_obj = false;
				node_to_move->setMaterialFlag(video::EMF_LIGHTING, false);
				node_to_move = 0;
			}
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_W && event.KeyInput.PressedDown)
		{				
			if(move_obj)
			{
				vector3df temp_pos = node_to_move->getPosition();
				temp_pos.Z -= 2;
				node_to_move->setPosition(temp_pos);
			}
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_S && event.KeyInput.PressedDown)
		{				
			if(move_obj)
			{
				vector3df temp_pos = node_to_move->getPosition();
				temp_pos.Z += 2;
				node_to_move->setPosition(temp_pos);
			}
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_A && event.KeyInput.PressedDown)
		{				
			if(move_obj)
			{
				vector3df temp_pos = node_to_move->getPosition();
				temp_pos.X += 2;
				node_to_move->setPosition(temp_pos);
			}
		}

		if(event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_KEY_D && event.KeyInput.PressedDown)
		{				
			if(move_obj)
			{
				vector3df temp_pos = node_to_move->getPosition();
				temp_pos.X -= 2;
				node_to_move->setPosition(temp_pos);
			}
		}



		return false;
}

void MyEventReceiver::Set_Reload(bool to_reload)
{
	reload = to_reload;
}

bool MyEventReceiver::Get_Reload()
{
	return reload;
}

int MyEventReceiver::Get_Cycle()
{
	return cycle_point;
}

int MyEventReceiver::Get_Size()
{
	return path_size;
}

void MyEventReceiver::Set_Size(int in_Amount)
{
	path_size = in_Amount;
}

bool MyEventReceiver::Get_Draw_Path()
{
	return draw_path;
}

bool MyEventReceiver::Get_Clear()
{
	return clear_path;
}

void MyEventReceiver::Set_Clear()
{
	clear_path = false;
}